package com.example.demo.model.meta;

import com.baidu.bas.annotation.Erupt;
import com.baidu.bas.annotation.EruptField;
import com.baidu.bas.annotation.sub_field.Edit;
import com.baidu.bas.annotation.sub_field.EditType;
import com.baidu.bas.annotation.sub_field.View;
import com.baidu.bas.annotation.sub_field.sub_edit.Search;
import com.baidu.bas.upms.model.base.HyperModel;
import com.deepzero.mmma.api.enums.FieldType;
import com.example.demo.model.custom.AccountDataProxy;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;
import java.io.Serializable;

/**
 * 字段定义
 * @author liuzhenchuan@foxmail.com
 * @date 2020/11/20
 */
@Setter
@Getter
@Erupt(name = "列",orderBy = "id desc")
@Table(name = "ma_meta_column")
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@DynamicUpdate
@NoArgsConstructor
@Accessors(chain = true)
public class Column extends HyperModel {
    /**
     * 字段名. 格式英文字符,数字,下划线
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String name;
    /**
     * 中文名称
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String cname;
    /**
     * 拼音 (用于提示)
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.HIDDEN)
    )
    protected String pinyin;
    /**
     * 字段类型
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    @Enumerated(value = EnumType.STRING)
    protected FieldType type = FieldType.STRING;
    /**
     * 维度
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean isDim = false;
    /**
     * 指标
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean isMetric = false;
    /**
     * 脱敏/加密
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String encodeMethod;
    /**
     * 字典
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean hasDict = false;
    /**
     * 使用中
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean enable = true;
    /**
     * 备注
     */
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.TEXTAREA)
    )
    protected String remarks;

    @EruptField(
            views = @View(title = "ID类型", column = "name"),
            edit = @Edit(title = "ID类型", type = EditType.REFERENCE_TABLE, search = @Search)
    )
    @OneToOne(optional = true)
    @JoinColumn(foreignKey = @ForeignKey(name = "fk_identity"))
    protected Identity identity;

    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.NUMBER)
    )
    protected Integer position = 100;

    @ManyToOne
    @JoinColumn(name = "logical_table_id",foreignKey = @ForeignKey(name = "fk_logical_table_2"),nullable = false, updatable = false, insertable = true)
    @EruptField(
            views = {
                    @View(title = "逻辑表", column = "name"),
                    @View(title = "事件类型", column = "isFact"),
                    @View(title = "租户", column = "tenancy")
            },
            edit = @Edit(title = "逻辑表", type = EditType.REFERENCE_TABLE, search = @Search)
    )
    protected LogicalTable logicalTable;

    public Column(String name, String cname) {
        this.name = name;
        this.cname = cname;
        this.type = FieldType.STRING;
    }

    public Column(String name, FieldType type) {
        this.name = name;
        this.cname = name;
        this.type = type;
    }

    public Column(String name, String cname, FieldType type) {
        this.name = name;
        this.cname = cname;
        this.type = type;
    }
}
